var app = sm("do_App");
var page = sm("do_Page");
var nf = sm("do_Notification");
var hashdata = mm("do_HashData");
var root = ui("$");
var http = require("helper/http");
var openHelper = require("helper/open");
var imHelper = require("helper/im");
var circleUrl = require("url/circle");
var friendHelper = require("helper/friend");

var httpUrl4Detail = circleUrl.url.member.detail4manage;

var isUpdate = false;
var id = null;
var memberId = null;
var circleId = null;
var type = null;
var isFriend = false;//是否好友
page.on("back", function() {
	app.closePage({member_update:isUpdate});
});

page.on("loaded", function() {
	id = page.getData().id;
	getMemberInfo();
});
var headerAout = ui("headerAout");
//加载头部
var addheader = headerAout.add("header", "source://view/reuse/header.ui", 0, 0);
var headerLeft = ui(addheader + ".headerLeft");
headerLeft.on("touch", function() {
	app.closePage({member_update:isUpdate});
});
ui(addheader + ".labelTitle").text = "";//呢称


var layoutMain = ui("layoutMain");
/*移出圈子*/
var addMove = layoutMain.add("move", "source://view/reuse/form/mutiLabel.ui",0,0);
var move = ui("move");
move.bindData(hashdata);
hashdata.addData({
    "labelTitle" : "移出圈子",
    "labelText":"",
    "imgForwardVisible": false
});
move.refreshData();
move.on("touch", function(data, e) {
	if(type=='1'){//圈主
		nf.toast("不能踢走圈主");
	}else if(type=='2'){
		nf.toast("不能踢走管理员");
	}else{
		nf.confirm("你确定要踢走该成员吗?", "", "确定", "取消", function(data, e) {
			if (data == 1) {
				http.post(circleUrl.url.member.kickMember, {memberId:memberId,circleId:circleId}, function(data) {
					nf.toast("踢人成功");
					app.closePage({kick_member:true});
				});
			}
		})
		
	}
});

/*设置禁言
var addBanned = layoutMain.add("banned", "source://view/reuse/form/mutiLabel.ui",0,0);
var banned = ui("banned");
banned.bindData(hashdata);
hashdata.addData({
    "labelTitle" : "设置禁言",
    "labelText":"",
    "imgForwardVisible": false
});
banned.refreshData();
*/
/*消息提示设置
var addMessage = layoutMain.add("message", "source://view/reuse/form/mutiLabel.ui",0,0);
var message = ui("message");
message.bindData(hashdata);
hashdata.addData({
    "labelTitle" : "消息提示设置",
    "labelText":"未设置",
    "imgForwardVisible": false
});
message.refreshData();
*/
/*允许发布投票*/
var addSwitch4vote = layoutMain.add("switch4vote","source://view/reuse/form/mutiSwitch.ui");
var switch4vote = ui("switch4vote");
switch4vote.bindData(hashdata);
hashdata.addData({"labelTitle" : "允许发布投票"});
switch4vote.refreshData();
switch4vote.on("switchtag", function(data) {
	if (data == 0) {// 关闭，禁止发布
		http.post(circleUrl.url.member.unAuthAddVote, {memberId:memberId,circleId:circleId}, function(data) {
			//nf.toast("撤权【发布投票】成功");
		});
	}
	if (data == 1) {// 打开，允许发布
		http.post(circleUrl.url.member.authAddVote, {memberId:memberId,circleId:circleId}, function(data) {
			//nf.toast("授权【发布投票】成功");
		});
	}
});

/*允许发布活动*/
var addSwitch4activity = layoutMain.add("switch4activity","source://view/reuse/form/mutiSwitch.ui");
var switch4activity = ui("switch4activity");
switch4activity.bindData(hashdata);
hashdata.addData({"labelTitle" : "允许发布活动"});
switch4activity.refreshData();
switch4activity.on("switchtag", function(data) {
	if (data == 0) {// 关闭，禁止发布
		http.post(circleUrl.url.member.unAuthAddActivity, {memberId:memberId,circleId:circleId}, function(data) {
			//nf.toast("撤权【发布活动】成功");
		});
	}
	if (data == 1) {// 打开，允许发布
		http.post(circleUrl.url.member.authAddActivity, {memberId:memberId,circleId:circleId}, function(data) {
			//nf.toast("授权【发布活动】成功");
		});
	}
});

/*设置管理员*/
var addSwitch4admin = layoutMain.add("switch4admin","source://view/reuse/form/mutiSwitch.ui");
var switch4admin = ui("switch4admin");
switch4admin.bindData(hashdata);
hashdata.addData({"labelTitle" : "设为管理员"});
switch4admin.refreshData();
switch4admin.on("switchtag", function(data) {
	if (data == 0) {// 关闭，删除管理员身份
		http.post(circleUrl.url.member.cancleCircleManager, {memberId:memberId,circleId:circleId}, function(data) {
			//nf.toast("取消管理员成功");
			type = 3;//成员类型:1-群主;2-管理员;3-普通成员
		});
	}
	if (data == 1) {// 打开，任命管理员身份
		http.post(circleUrl.url.member.setCircleManager, {memberId:memberId,circleId:circleId}, function(data) {
			//nf.toast("设置管理员成功");
			type = 2;//成员类型:1-群主;2-管理员;3-普通成员
		});
	}
});

/*加入时间*/
var addJoinTime = layoutMain.add("joinTime", "source://view/reuse/form/mutiLabel.ui",0,0);
var joinTime = ui("joinTime");
joinTime.bindData(hashdata);
hashdata.addData({
    "labelTitle" : "加入时间",
    "labelText":"2018-06-06",
    "imgForwardVisible": false
});
joinTime.refreshData();

/*成员头衔*/
var addMemberGrade = layoutMain.add("memberGrade", "source://view/reuse/form/mutiLabel.ui",0,0);
var memberGrade = ui("memberGrade");
memberGrade.bindData(hashdata);
hashdata.addData({
    "labelTitle" : "成员头衔",
    "labelText":"未设置",
    "imgForwardVisible": false
});
memberGrade.refreshData();

var addPicker = ui("alayoutRoot").add("picker","source://view/reuse/form/oneCategory.ui", 0, 0);
//var picker = ui("picker");
memberGrade.on("touch", "", 300, function() {
	if(type=='1' || type=='2'){//圈主或管理员
		nf.toast("不能为圈主或管理员赋予头衔!");
		return;
	}	
	var ptitle = {"title" : "选择头衔","id" : "selectMemberGrade"};
	http.post(circleUrl.url.memberGrade.list4circle, {"circleId":circleId}, function(data) {
		var gradeList = data.gradeList;
		var gradeList4Category = [];
		for(var i in gradeList){
			gradeList4Category.push({"id":gradeList[i].gradeCode,name:gradeList[i].gradeName});
		}
		if(gradeList4Category!=null && gradeList4Category.length>0){
			var pcount = [ ptitle, gradeList4Category ];
			page.fire("oneCategoryList", pcount);
			page.fire("oneCategory", "true");
		}
	});
});
page.on("refreshOneCategory", function(data) {
	if ("selectMemberGrade" == data[1]) {//选择头衔
		var gradeCode = data[0].oneid;//新的"头衔代码"
		var gradeName = data[0].onename;//新的"头衔名称"
		if(gradeCode!=null && gradeCode!="" && gradeCode > "0"){
			http.post(circleUrl.url.member.setGrade, {"circleId":circleId,"memberId": memberId,"gradeCode":gradeCode}, function(data) {
				ui(addMemberGrade +".labelText").text = "【"+gradeName+"】";
				isUpdate = true;
			});
		}
	}
});

/*圈名片
var addCard = layoutMain.add("card", "source://view/reuse/form/mutiLabel.ui",0,0);
var card = ui("card");
card.bindData(hashdata);
hashdata.addData({
    "labelTitle" : "圈名片",
    "labelText":"未设置",
    "imgForwardVisible": false
});
card.refreshData();
*/
/*查看用户信息*/
var addViewInfo = layoutMain.add("viewInfo", "source://view/reuse/form/mutiLabel.ui",0,0);
var viewInfo = ui("viewInfo");
viewInfo.bindData(hashdata);
hashdata.addData({
    "labelTitle" : "查看用户信息",
    "labelText":"",
    "imgForwardVisible": false
});
viewInfo.refreshData();
viewInfo.on("touch", function(data, e) {
	if(memberId !=null){
		openHelper.openPersonal(memberId);
	}
});
/*************function***************/
function getMemberInfo(){
	http.post(httpUrl4Detail,{id:id},function(data){
		var member = data.member;
		isFriend = data.isFriend;
		if(isFriend){
			ui("imgFriend").source="source://image/menu/userdel.png";
			ui("lableFriend").text="删除好友";
		}else{
			ui("imgFriend").source="source://image/menu/useradd.png";
			ui("lableFriend").text="加为好友";
		}
		if(member){
			//deviceone.print(JSON.stringify(member))
			circleId = member.circleId;
			memberId = member.memberId;
			type = member.type;//成员类型:1-圈主;2-管理员;3-普通成员
			if(type =="2"){
				switch4admin.fire("switchzt", [ 1, " ", " " ]);//管理员
			}else{
				switch4admin.fire("switchzt", [ 0, " ", " " ]);//非管理员
			}
			var canAddActivity = member.canAddActivity;//可否发布活动:1-是;2-否
			if(canAddActivity=="1"){
				switch4activity.fire("switchzt", [ 1, " ", " " ]);
			}else{
				switch4activity.fire("switchzt", [ 0, " ", " " ]);
			}
			var canAddVote = member.canAddVote;//可否发布投票:1-是;2-否
			if(canAddVote=="1"){
				switch4vote.fire("switchzt", [ 1, " ", " " ]);
			}else{
				switch4vote.fire("switchzt", [ 0, " ", " " ]);
			}
			ui(addJoinTime +".labelText").text = member.strNewAt.substring(0,10);
			ui(addheader + ".labelTitle").text = member.memberName;
			ui("imageViewLogo").source = member.fullMemberImg;
			if(member.sex==2){//女
				ui("labelSex").text="♀";
				ui("layoutSex").bgColor = "F883B8FF";
			}else{//男
				ui("labelSex").text="♂";
				ui("layoutSex").bgColor = "3EC8B1FF";
			}
			setMemberGrade(member.gradeName);
		}
	});
}

ui("aLayoutUserIm").on("touch", function(data, e) {
	if(memberId != null){
		imHelper.toChat(memberId);
	}
})
//添加好友
ui("aLayoutFriend").on("touch", function(data, e) {
	if(isFriend){
		//如果已为好友，再次点击则删除好友
		nf.confirm("您确定要删除这个好友吗?","删除好友", "确定", "取消", function(data, e) {
			if (data == 1) {
				friendHelper.deleteFriend(memberId,function(data){
					isFriend = false;
					ui("imgFriend").source="source://image/menu/useradd.png";
					ui("lableFriend").text="加为好友";
					nf.toast("删除好友成功");
					imHelper.removeHistoryMessage(memberId);
				});
			}
		})
	}else{
		friendHelper.addFriendApply(memberId);
	}
})


function setMemberGrade(vsGradeName){
	if(type=='1'){//圈主
		ui(addMemberGrade +".labelText").text = "圈主";
	}else if(type=='2'){
		ui(addMemberGrade +".labelText").text = "管理员";
	}else{
		if(vsGradeName=="" || vsGradeName==null){
			ui(addMemberGrade +".labelText").text = "未设置";
		}else{
			ui(addMemberGrade +".labelText").text = "【"+vsGradeName+"】";//头衔
		}
		
	}
}